﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace Ams.Extensions
{
    public static class StringExtensions
    {
        public static string SplitCamelCase(this string str)
        {
            return Regex.Replace(str ?? String.Empty, "(?<=[a-z])([A-Z])", " $1", RegexOptions.Compiled).Trim();
        }

        public static string Titlize(this string str)
        {
            var wordsToLowerCase = "a,and,an,aboard,about,across,amid,among,anti,around,as,at,before,behind,below,beneath,beside,besides,between,beyond,but,by,despite,down,for,from,in,inside,into,like,minus,near,of,off,on,onto,opposite,outside,over,past,per,since,the,than,through,to,toward,towards,under,unlike,until,up,upon,versus,via,with,within,without".SplitCsv();
            var words = (str ?? String.Empty).SplitCamelCase().SplitWords().Select(i => wordsToLowerCase.Contains(i.ToLower()) ? i.ToLower() : i);
            var titlizedStr = words.Join(" ");
            return titlizedStr;
        }

        public static string[] SplitWords(this string str)
        {
            return (str ?? String.Empty).Split(new[] { " " }, StringSplitOptions.None);
        }
        public static string[] SplitCsv(this string str)
        {
            return (str ?? String.Empty).Split(new[] { "," }, StringSplitOptions.None);
        }
        public static string[] SplitCsv(this string str, string spliter)
        {
            return (str ?? String.Empty).Split(new[] { spliter }, StringSplitOptions.None);
        }
        public static string Join<T>(this IEnumerable<T> str, string joiner)
        {
            return String.Join((joiner ?? String.Empty), str.Select(i => i.ToString()));
        }
        public static string WrapInTag(this string str, string tagName)
        {
            return String.Format("<{0}>{1}</{0}>", tagName, (str ?? String.Empty));
        }

        public static bool Matches(this string str, string regexPattern)
        {
            return Regex.IsMatch(str ?? String.Empty, regexPattern, RegexOptions.IgnoreCase);
        }
        public static bool Matches(this string str, string regexPattern, RegexOptions options)
        {
            return Regex.IsMatch(str ?? String.Empty, regexPattern, options);
        }
    }
}
